﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fashion_API;

namespace Fashion.BackEnd.Tool.ManagePage
{
    public partial class BackEndPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            using (DALDataContext d = new DALDataContext())
            {
                grdPage.DataSource = d.BackEndPages;
                grdPage.DataBind();
            }
        }

        protected void grdPage_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DropDownList cboParentPage = (DropDownList) e.Item.FindControl("cboParentPage");
                BindDataCboParentPage(cboParentPage);
                if (DataBinder.Eval(e.Item.DataItem, "ParentId") != null)
                    cboParentPage.SelectedValue = DataBinder.Eval(e.Item.DataItem, "ParentId").ToString();
            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                DropDownList cboParentPage = (DropDownList) e.Item.FindControl("cboParentPage");
                BindDataCboParentPage(cboParentPage);
            }
        }

        private void BindDataCboParentPage(DropDownList cbo)
        {
            using (DALDataContext d = new DALDataContext())
            {
                List<Fashion_API.BackEndPage> list = d.BackEndPages.Where(i => i.ParentId == null).ToList();
                Fashion_API.BackEndPage backEndPage = new Fashion_API.BackEndPage();
                backEndPage.Id = 0;
                backEndPage.PageName = "----- None -----";
                list.Insert(0,backEndPage);
                cbo.DataSource = list;
                cbo.DataTextField = "PageName";
                cbo.DataValueField = "Id";
                cbo.DataBind();
            }
        }
    }
}